package com.REPDPMSystem.Controller;

import com.REPDPMSystem.Entity.MyFile;
import com.REPDPMSystem.Service.EmployeeService;
import com.REPDPMSystem.Service.FileService;
import com.REPDPMSystem.Service.ProjectService;
import com.REPDPMSystem.Utils.R;
import com.REPDPMSystem.Entity.Employee;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class EmployeeController {
    //TODO:查询团队员工的工作负荷(正在做的，每个员工都可查) project_status==xxx ok
    //TODO:老板可以了解公司的指定时间段的业绩（文件与项目不同权值   0.2  0.8 ?）和员工的工作情况
    //TODO:系统记录每个员工所承担的项目，包括已完成项目、正在执行的项目和主管新发布的项目，该记录作为计算员工绩效主要依据之一
    @Autowired
    EmployeeService employeeService;
    @Autowired
    ProjectService projectService;
    @Autowired
    private FileService fileService;
    //老板，前台，主管
    //OK

    @CrossOrigin
    @PostMapping("/GET/EmployeeNameById")
    public R getEmployeeNameById(@RequestParam(value = "employee_id")String employeeIdarr){
        Map<String, Object> map = JSON.parseObject(employeeIdarr, new TypeReference<Map<String, Object>>() {});
        System.out.println((String)map.get("model_employee_id"));

        return R.ok()
                .data("model_employee_name",new JSONArray(employeeService.getEmployeeNameById((String)map.get("model_employee_id"))))
                .data("rendering_employee_name",new JSONArray(employeeService.getEmployeeNameById((String)map.get("rendering_employee_id"))))
                .data("post_production_employee_name",new JSONArray(employeeService.getEmployeeNameById((String)map.get("post_production_employee_id"))));
    }


    @CrossOrigin
    @GetMapping("/GET/EmployeeInfo")
    public R getEmployeeInfo(){
        return R.ok().data("EmployeeInfoList",new JSONArray(employeeService.getEmployeeInfo()));
    }

    //OK
    @CrossOrigin
    @GetMapping("/GET/EmployeeInfoByName")
    public R getEmployeeInfoByName(@RequestParam(value = "name")String name){
        return R.ok().data("EmployeeInfoList",new JSONArray(employeeService.getEmployeeInfoByName(name)));
    }
    //OK
    @CrossOrigin
    @PostMapping("/POST/CreateEmployeeInfo")
    public R createEmployeeInfo(@RequestBody Employee employee){
        return employeeService.createEmployee(employee)?R.ok() : R.error().message("创建用户数据失败！");
    }
    //OK
    @CrossOrigin
    @PostMapping("/POST/ChangeEmployeeInfo")
    public R changeEmployeeInfo(@RequestBody Employee employee){
        return employeeService.changeEmployeeInfo(employee) ? R.ok() : R.error().message("更新用户数据失败！");
    }
    @CrossOrigin
    @PostMapping("/GET/DepartmentEmployeeList")
    public R getDepartmentEmployeeList(@RequestParam(value= "employee_department") int employee_department){
        return R.ok().data("DepartmentEmployeeList",employeeService.getDepartmentEmployee(employee_department));

    }
    @CrossOrigin
    @PostMapping("/GET/TeamWorkLoad")
    public R getTeamWorkLoad(@RequestParam(value= "employee_department") int employee_department){
        //已完成，正在做 统计图
        return R.ok()
                .data("DoneCounts",projectService.getDoneProjectStep(employee_department))
                .data("DoingCounts",projectService.getDoingProjectStep(employee_department));
    }
    //OK
    @CrossOrigin
    @PostMapping("/POST/ChangePassword")
    public R changePassword(@RequestParam(value= "userName")String userName,@RequestParam(value= "passWord")String passWord,@RequestParam(value= "newPassword")String newPassword){

        return employeeService.changePassword(userName,passWord,newPassword) ? R.ok():R.error().message("用户名或旧密码错误!");
    }
    //OK
    @CrossOrigin
    @PostMapping("/POST/login")
    public R login(@RequestParam(value = "userName")String userName,@RequestParam(value = "passWord")String passWord){//获取个人信息
         List<Employee> Elist = employeeService.getEmployeeInfo(userName,passWord);
         return Elist.isEmpty() ? R.error().message("用户名或密码错误！") : R.ok().data("EmployeeInfo",new JSONArray(Elist));

    }

    @CrossOrigin
    @PostMapping("/POST/GetCountUploadedFilesByEmployeeAndTimePeriod")
    public R getCountUploadedFilesByEmployeeAndTimePeriod(@RequestParam(value = "employeeId")String employeeId,@RequestParam(value = "startTime") long startTime,@RequestParam(value = "endTime") long endTime){
        return R.ok().data("Counts",fileService.countUploadedFilesByEmployeeAndTimePeriod(employeeId,startTime,endTime));
    }

    @CrossOrigin
    @PostMapping("/POST/GetCountDoneProjectByEmployeeAndTimePeriod")
    public R getCountDoneProjectByEmployeeAndTimePeriod(@RequestParam(value = "employeeId")String employeeId,@RequestParam(value = "startTime") long startTime,@RequestParam(value = "endTime") long endTime){
        int employeeDepartment = employeeService.getEmployeeDepartment(employeeId);
        int e = employeeService.getEmployeePositionById(employeeId).ordinal();
        return R.ok().data("Counts",projectService.countDoneProjectByEmployeeAndTimePeriod(e,employeeId,employeeDepartment,startTime,endTime));
    }

    @CrossOrigin
    @PostMapping("/POST/GetDoneProjectByEmployeeAndTimePeriodList")
    public R getDoneProjectByEmployeeAndTimePeriodList(@RequestParam(value = "employeeId")String employeeId,@RequestParam(value = "startTime") long startTime,@RequestParam(value = "endTime") long endTime){
        int employeeDepartment = employeeService.getEmployeeDepartment(employeeId);
        int e = employeeService.getEmployeePositionById(employeeId).ordinal();
        return R.ok().data("Counts",projectService.getDoneProjectByEmployeeAndTimePeriod(e,employeeId,employeeDepartment,startTime,endTime));
    }

    @CrossOrigin
    @PostMapping("/POST/GetUploadedFilesByEmployeeAndTimePeriodList")
    public R getUploadedFilesByEmployeeAndTimePeriodList(@RequestParam(value = "employeeId")String employeeId,@RequestParam(value = "startTime") long startTime,@RequestParam(value = "endTime") long endTime){
        return R.ok().data("Counts",fileService.getUploadedFilesByEmployeeAndTimePeriod(employeeId,startTime,endTime));
    }


}
